using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Entites;
namespace DAL
{
  public  class DiaDiemDAL
    {
        
            private SqlConnection cn = null;
        public DiaDiemDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
       public void Insert (DiaDiem dd )
        {
            SqlCommand cm = new SqlCommand("DiaDiem_insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@TenDD",dd.tendd);
           cm.Parameters.AddWithValue("@DiaChi",dd.diachi);
            cm.ExecuteNonQuery();
        }
        public void Update(DiaDiem dd)
        {
            SqlCommand cm = new SqlCommand("DiaDiem_update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaDD", dd.madd);
            cm.Parameters.AddWithValue("@TenDD",dd.tendd);
           cm.Parameters.AddWithValue("@DiaChi",dd.diachi);
          cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("DiaDiem_delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaDD", iD);
            cm.ExecuteNonQuery();
        }

        public tbDiaDiemCollection Get(string where, int pageIndex, int pageSize,
            string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("DiaDiem_get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbDiaDiemCollection ddCollection = new tbDiaDiemCollection();

            while (dr.Read())
            {
                DiaDiem diadiem = new DiaDiem();
                diadiem.madd = int.Parse(dr.GetValue(0).ToString());
                diadiem.tendd = dr.GetValue(1).ToString();
                diadiem.diachi = dr.GetValue(2).ToString();
                ddCollection.Add(diadiem);
             }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return ddCollection;
        }
    }
    
}
